﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Web.UI;
using DevExpress.Web.ASPxHeadline;
using DevExpress.Web.ASPxClasses;
using DevExpress.Web.ASPxCallback;
using DevExpress.Web.ASPxGridView;

using LMD.Constant;
using LMD.Lib.DataClass;
using LMD.Lib.Interface;

public partial class CVList : ICPRelationshipPage
{
    public CCData ccData = new CCData();
    public CVData cvData = new CVData();
    #region Properties
    public String m_sxMode
    {
        get
        {
            if (!String.IsNullOrEmpty(Request.QueryString["mode"]))
            {
                return Context.ReadQuery("mode").ToUpper();
            }
            else
            {
                return "PLANNED";
            }
        }
    }
    #endregion Properties

    #region Event Handlers

    protected override void Page_Load(object sender, EventArgs e)
    {
        m_lsPermittedRoles.Add(ICPUserRole.CAR);
        base.Page_Load(sender, e);

        Page.Title = "ICP Lintasarta: Relationship, Customer Visit - Visit List";
        if (!IsPostBack && !IsCallback)
        {
            
        }
        InitVisitList(m_sxMode);
    }
    protected void cbkList_Callback(object sender, CallbackEventArgs e)
    {
        System.Threading.Thread.Sleep(KGlobal.AJAX_WAIT_TIME);
        e.Result = HandleList(e.Parameter);
    }
    protected void dgVisitView_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e)
    {
        if(e.Parameters.StartsWith("REFRESH"))
        {
            InitVisitList(m_sxMode);
        }
        else if (e.Parameters.StartsWith("CANCEL"))
        {
            HandleCancelation(e.Parameters);
        }
        else if (e.Parameters.ToUpper().StartsWith("FILTER"))
        {
            HandleFilter(e.Parameters);
        }
        else
        {
            HandleAll();
        }
        
    }
    protected void btnExport_Click(object sender, EventArgs e)
    {
        try
        {
            String sxFilename = String.Format("Customer Visit List - {0}", DateTime.Now.ToString());
            dgExporter.WriteXlsToResponse(sxFilename);
        }
        catch (Exception x)
        {
            string strError = x.Message.ToString();
        }
        
    }
    #endregion Event Handlers

    #region Custom Methods
    protected override void DoSearch(String sxSearchQuery)
    {
        throw new NotImplementedException();
    }
    public void InitVisitList(String sxMode)
    {
        DateTime dtmStart = Session[KSession.CV_STARTDATE] != null ?
            Convert.ToDateTime(Session[KSession.CV_STARTDATE]) :
            new DateTime();
        DateTime dtmEnd = Session[KSession.CV_ENDDATE] != null ?
            Convert.ToDateTime(Session[KSession.CV_ENDDATE]) :
            new DateTime();
        int iStartDate = dtmStart.Day;
        int iEndDate = dtmEnd.Day;
        int iStartMonth = dtmStart.Month;
        int iStartYear = dtmStart.Year;
        int iEndMonth = dtmEnd.Month;
        int iEndYear = dtmEnd.Year;
        //Cached.CustomerVisitTable cachedCV = new Cached.CustomerVisitTable();
        //DataTable dtVisitSummary = cachedCV.GetVisitSummary(sxCarId, iStartMonth, iStartYear, iEndMonth, iEndYear);
        Cached.CustomerVisitTable cachedCV = new Cached.CustomerVisitTable();
        DataTable dtVisitList = new DataTable();

        if (dtmStart != null && dtmEnd != null)
        {
            dtVisitList = cachedCV.GetVisitList(sxMode, iStartDate, iEndDate, iStartMonth, iStartYear, iEndMonth, iEndYear);
        }

        dgVisitView.DataSource = dtVisitList;
        dgVisitView.DataBind();
        if (sxMode.Equals("PLANNED"))
        {
            dgVisitView.Columns["VIEW"].Visible = false;
        }
        else
        {
            dgVisitView.Columns["EDIT"].Visible = false;
            dgVisitView.Columns["REALIZE"].Visible = false;
            dgVisitView.Columns["CANCEL_VISIT"].Visible = false;
        }
    }

    private string HandleList(string sxParam)
    {
        String[] sxSplitted = sxParam.Split(new String[] { KGlobal.DELIMITER }, StringSplitOptions.None);

        String sxOutput = String.Empty;
        int iCVID = -1;
        DateTime dtmVisitDatetime = DateTime.MinValue;
        
        switch (sxSplitted[0].ToUpper())
        {
            case "EDIT":
                iCVID = Convert.ToInt32(sxSplitted[1]);
                dtmVisitDatetime = DBQuery.CustomerVisitTable.GetPlannedDateById(iCVID);
                sxOutput = CVManager.BuildEditor_EditPlannedDate(iCVID.ToString(), iCVID, dtmVisitDatetime);
                break;
            case "SAVE":
                iCVID = Convert.ToInt32(sxSplitted[1]);
                dtmVisitDatetime = Convert.ToDateTime(sxSplitted[2]);
                DBQuery.CustomerVisitTable.EditVisitDatetime(iCVID, dtmVisitDatetime);
                InitVisitList(m_sxMode);
                break;
        }

        return sxOutput;
    }
    private void HandleCancelation(string sxParam)
    {
        String[] sxSplitted = sxParam.Split(new String[] { KGlobal.DELIMITER }, StringSplitOptions.None);
        int iCVID = Convert.ToInt32(sxSplitted[1]);
        String sxReason = sxSplitted[2].ToString();
        DBQuery.CustomerVisitTable.CancelVisit(iCVID, sxReason);
        Cached.CustomerVisitTable cachedCV = new Cached.CustomerVisitTable();
        cachedCV.RemoveCache();
        //InitVisitList(m_sxMode);
    }

    //add by rista
    public void HandleFilter(String sxParameter)
    {
        String[] sxSplitted = sxParameter.Split(new String[] { KGlobal.DELIMITER }, StringSplitOptions.None);

        DateTime dtmStart = DateTime.Now;
        DateTime dtmEnd = DateTime.Now;
       
        if (!String.IsNullOrEmpty(sxSplitted[1]))
        {
            dtmStart = Convert.ToDateTime(sxSplitted[1]);
        }
        if (!String.IsNullOrEmpty(sxSplitted[2]))
        {
            dtmEnd = Convert.ToDateTime(sxSplitted[2]);
        }
        Session[KSession.CV_STARTDATE] = dtmStart;
        Session[KSession.CV_ENDDATE] = dtmEnd;
        InitVisitList(m_sxMode);
    }

    public void HandleAll()
    {
        Session[KSession.CV_STARTDATE] = new DateTime();
        Session[KSession.CV_ENDDATE] = new DateTime();
        InitVisitList(m_sxMode);
    }
    #endregion Custom Methods
}
